package org.jeecg.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@ConfigurationProperties(prefix="threadpool")
public class TaskExecutePool {

    @Value("${threadpool.core-pool-size}")
    private int corePoolSize;

    @Value("${threadpool.max-pool-size}")
    private int maxPoolSize;

    @Value("${threadpool.queue-capacity}")
    private int queueCapacity;

    @Value("${threadpool.keep-alive-seconds}")
    private int keepAliveSeconds;


    @Value("${threadpool2.core-pool-size}")
    private int corePoolSize2;
    @Value("${threadpool2.max-pool-size}")
    private int maxPoolSize2;
    @Value("${threadpool2.queue-capacity}")
    private int queueCapacity2;
    @Value("${threadpool2.keep-alive-seconds}")
    private int keepAliveSeconds2;



    @Bean(name="threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setThreadGroupName("curve-group");
        pool.setKeepAliveSeconds(keepAliveSeconds);
        pool.setCorePoolSize(corePoolSize);//核心线程池数
        pool.setMaxPoolSize(maxPoolSize); // 最大线程
        pool.setQueueCapacity(queueCapacity);//队列容量
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return pool;
    }

    @Bean(name="threadPoolTaskExecutor2")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor2(){
        ThreadPoolTaskExecutor pool2 = new ThreadPoolTaskExecutor();
        pool2.setThreadGroupName("curve-group2");
        pool2.setKeepAliveSeconds(keepAliveSeconds2);
        pool2.setCorePoolSize(corePoolSize2);//核心线程池数
        pool2.setMaxPoolSize(maxPoolSize2); // 最大线程
        pool2.setQueueCapacity(queueCapacity2);//队列容量
        pool2.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return pool2;
    }
}
